package org.csu.mybigpro.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.csu.mybigpro.domain.Replies;

public interface RepliesMapper extends BaseMapper<Replies> {

    default Page<Replies> selectPageList(Long threadId, int pageNo, int pageSize){

        Page<Replies> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Replies> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Replies::getThreadId,threadId);
        wrapper.eq(Replies::getIsDelete,"0");
        return selectPage(page, wrapper);
    }

    default  int selectCountByThreadId(Long threadId){
        LambdaQueryWrapper<Replies> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Replies::getThreadId,threadId);
        wrapper.eq(Replies::getIsDelete,"0");
        return Math.toIntExact(selectCount(wrapper));
    }

    default int thumbsUp(int replyId){
        LambdaUpdateWrapper<Replies> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Replies::getId,replyId)
                .setSql("thumbs_up_count = thumbs_up_count +1");
        return update(null,wrapper);
    }



    default int thumbsDown(int replyId){
        LambdaUpdateWrapper<Replies> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Replies::getId,replyId)
                .setSql("thumbs_up_count = thumbs_up_count -1");
        return update(null,wrapper);
    }

    default Replies getRepliesById(int replyId){
        LambdaQueryWrapper<Replies> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Replies::getId,replyId);
        return selectOne(wrapper);
    }

    default int delReply(int replyId){
        LambdaUpdateWrapper<Replies> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Replies::getId, replyId);
        wrapper.set(Replies::getIsDelete,"1");
        return update(wrapper);
    }
}
